/*
 * C4.7.2.c
 *
 *  Created on: 2012/12/04
 *      Author: ming
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rule_info.h"
#include "core.h"
#include "trace.h"
#include "TraceAst.h"

static const PSZ RuleID = "C4.7.2";

#define FLAG_LEFT	1
#define FLAG_RIGHT	2
#define FLAG_BOTH	3

static const PSZ MESSAGE_ENG =
		"Equality check by floating type('%s' 's %s operator)";
static const PSZ MESSAGE_JPN = "浮動小数点式での等価・非等価テスト('%s'演算子の%s側)";
static const PSZ MESSAGE_CHS = "用了浮点型进行相等或不相等的比较('%s'演算子的%s侧)";

static const U2 RANK = MSG_RANK_ERR;

static PRULEOUT pRuleHead;
static PRULEOUT pRuleTail;

/******************************************************************
 * static function:
 * isExpFloating
 *******************************************************************/
static BOOL isExpFloating(AST exp) {
	AST type = 0;
	type = CExpression.getTypeInfo(exp);
	if (CTypeInfo.isFloating(type)) {
		return TRUE;
	}
	return FALSE;
}

/******************************************************************
 * main logic
 *******************************************************************/
static S4 RuleMain(U2 kind, AST index, U4 event) {
	INT result = RULE_RESULT_OK;
	PRULEOUT newNode = NULL;
	PRULEOUT tmpNode = NULL;
	PRULEOUT tmpNode1 = NULL;

	AST codeScp = 0;
	U4 expKind = 0;
	BOOL bLeft = FALSE;
	BOOL bRight = FALSE;
	PSZ opName = NULL;
	INT lrFlag = 0;

	if (kind != TR_KIND_EXPRESSION) {
		return EXIT_SUCCESS;
	}

	if (!(event & EVT_EXPRESSION)) {
		return EXIT_SUCCESS;
	}

	expKind = CExpression.getOpKind(index);
	if (CEqualityOpKind.isEqualOp(expKind)) {
		opName = "==";
	} else if (CEqualityOpKind.isUnequalOp(expKind)) {
		opName = "!=";
	} else if (CRelationalOpKind.isGreaterOrEqual(expKind)) {
		opName = ">=";
	} else if (CRelationalOpKind.isLessOrEqual(expKind)) {
		opName = "<=";
	} else {
		return EXIT_SUCCESS;
	}

	bLeft = isExpFloating(CExpression.getLeftNode(index));
	bRight = isExpFloating(CExpression.getRightNode(index));
	if (bLeft && !bRight) {
		result = RULE_RESULT_NG;
		lrFlag = FLAG_LEFT;
	} else if (!bLeft && bRight) {
		result = RULE_RESULT_NG;
		lrFlag = FLAG_RIGHT;
	} else if (bLeft && bRight) {
		result = RULE_RESULT_NG;
		lrFlag = FLAG_BOTH;
	} else {
		result = RULE_RESULT_OK;
	}

	if (result == RULE_RESULT_OK) {
		return EXIT_SUCCESS;
	}

	/*re-checked?*/
	tmpNode = pRuleHead;
	while (tmpNode != NULL ) {
		if (tmpNode->indexInfo == index) {
			if (result > RULE_RESULT_OK) {
				result = RULE_RESULT_OK;
			} else {
				if (tmpNode == pRuleHead) {
					pRuleHead = pRuleHead->next;
					free(tmpNode);
				} else if (tmpNode == pRuleTail) {
					tmpNode1->next = NULL;
					pRuleTail = tmpNode1;
					free(tmpNode);
				} else {
					tmpNode1->next = tmpNode->next;
					free(tmpNode);
				}
			}
			break;
		}
		tmpNode1 = tmpNode;
		tmpNode = tmpNode->next;
	}

	/*make result struct*/
	if (result > RULE_RESULT_OK) {
		newNode = (PRULEOUT) malloc(sizeof(RULEOUT));
		if (pRuleHead == NULL ) {
			pRuleHead = newNode;
		}

		codeScp = CExpression.getCodeScope(index);
		newNode->indexInfo = index;
		newNode->fileIndex = CCodeScope.getFileIndex(codeScp);
		newNode->beginLine = CCodeScope.getBeginLine(codeScp);
		newNode->beginCol = CCodeScope.getBeginLineOffset(codeScp);
		newNode->endLine = CCodeScope.getEndLine(codeScp);
		newNode->endCol = CCodeScope.getEndLineOffset(codeScp);
		newNode->ruleID = RuleID;
		newNode->errRank = RANK;

		if (lrFlag == FLAG_LEFT) {
			sprintf(newNode->msgBuf_eng, MESSAGE_ENG, opName, "left");
			sprintf(newNode->msgBuf_chs, MESSAGE_CHS, opName, "左");
			sprintf(newNode->msgBuf_jpn, MESSAGE_JPN, opName, "左");
		} else if (lrFlag == FLAG_RIGHT) {
			sprintf(newNode->msgBuf_eng, MESSAGE_ENG, opName, "right");
			sprintf(newNode->msgBuf_chs, MESSAGE_CHS, opName, "右");
			sprintf(newNode->msgBuf_jpn, MESSAGE_JPN, opName, "右");
		} else if (lrFlag == FLAG_BOTH) {
			sprintf(newNode->msgBuf_eng, MESSAGE_ENG, opName, "left and right");
			sprintf(newNode->msgBuf_chs, MESSAGE_CHS, opName, "左右");
			sprintf(newNode->msgBuf_jpn, MESSAGE_JPN, opName, "左右");
		}

		newNode->rfileIndex = 0;
		newNode->rlocLine = 0;
		newNode->next = NULL;

		if (pRuleTail != NULL ) {
			pRuleTail->next = newNode;
		}

		pRuleTail = newNode;
	}

	return EXIT_SUCCESS;
}

VOID C4_7_2_register(VOID) {
	PRULEMAN * pCurRule = &stRuleManager;
	PRULEMAN newRuleMan = (PRULEMAN) malloc(sizeof(RULEMAN));
	memset(newRuleMan, 0, sizeof(RULEMAN));
	newRuleMan->ruleID = RuleID;
	newRuleMan->ruleSwitch = OFF;
	newRuleMan->headOut = &pRuleHead;
	newRuleMan->tailOut = &pRuleTail;
	newRuleMan->ruleMain_cmd = NULL;
	newRuleMan->ruleMain_ast = RuleMain;
	newRuleMan->nextRule = NULL;
	if (stRuleManager == NULL ) {
		stRuleManager = newRuleMan;
	} else {
		while ((*pCurRule)->nextRule != NULL ) {
			pCurRule = &((*pCurRule)->nextRule);
		}
		(*pCurRule)->nextRule = newRuleMan;
	}
}

